let qiniu = require("qiniu");

// 七牛云ak
const accessKey = 'oqO2wCYo3odfOAajHvf5a6pZhnXF8UFtaOE0LaBh';

// 七牛云sk
const secretKey = 'MfZFCz4IqH3sKv-ZJvJqy0jGeJ245Qtx6tzp7fhP';

//
const mac = new qiniu.auth.digest.Mac(accessKey, secretKey);

let lastTime = 0;

/**
 * 
 * @param {Object} data 请求参数
 * @param {Function} callback 回调函数 
 */
exports.getToken = function (data, callback) {
    const expires = 7200;
    let uploadToken = null;
    if (new Date().getTime() - lastTime > expires * 1000 || uploadToken == null) {
        //自定义凭证有效期（示例2小时，expires单位为秒，为上传凭证的有效时间）
        var options = {
            scope: "shzy",
            // scope: "shzy-stu",
            expires
        };

        var putPolicy = new qiniu.rs.PutPolicy(options);
        uploadToken = putPolicy.uploadToken(mac);
    }

    callback({
        status: 1,
        msg: "OK",
        result_data: {
            token: uploadToken,
            // 把资源访问的地址前缀也返回去
            remoteAddr: "https://zh.forerunnercollege.com",
            // remoteAddr: "http://rdtin2w6i.hn-bkt.clouddn.com",
        }
    })
}